﻿using ConsoleApp_EXL;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET
{
    public static class GenerateFcBlock
    {
        public static void GenerateFcBlockFromTable(List<InputTableRow> rows, string? path)
        {
            using (FileStream exlFile = new FileStream(path + "/块_2.scl", FileMode.Create, FileAccess.ReadWrite))
            {
                StreamWriter writer = new StreamWriter(exlFile, Encoding.UTF8);  //创建文件
                                                                              //写入文件头
                writer.WriteLine("FUNCTION \"块_2\" : Void");
                writer.WriteLine("{ S7_Optimized_Access := 'TRUE' }");
                writer.WriteLine("VERSION : 0.1");
                writer.WriteLine();
                writer.WriteLine("BEGIN");
                writer.WriteLine();

                //写入程序
                var In = rows.Where(row => row.InOut == "In" & row.Name != @"/").GroupBy(row => row.Group);
                foreach (var iteam in In) 
                {
                    for (int i = 0; i < iteam.Count(); i++)
                    {
                        if (i == 0)
                        {
                            writer.WriteLine("\tREGION " + iteam.ToList()[i].Group);
                        }

                        if (iteam.ToList()[i].Group2 == @"/")
                        {
                            writer.WriteLine("\t\t\"数据块_2\".Input." + iteam.ToList()[i].Group + "." + iteam.ToList()[i].Name + " := \"" + iteam.ToList()[i].Adress + "\";" + "  //  " + iteam.ToList()[i].Notes);
                        }
                        else
                        {
                            writer.WriteLine("\t\t\"数据块_2\".Input." + iteam.ToList()[i].Group + "." + iteam.ToList()[i].Group2 + "." + iteam.ToList()[i].Name + " := \"" + iteam.ToList()[i].Adress + "\";" + "  //  " + iteam.ToList()[i].Notes);
                        }
                        
                        if (i == iteam.Count()-1)
                        {
                            writer.WriteLine("\tEND_REGION");
                        }
                    }
                }

                var Out = rows.Where(row => row.InOut == "Out" & row.Name != @"/").GroupBy(row => row.Group);
                foreach (var iteam in Out)
                {
                    for (int i = 0; i < iteam.Count(); i++)
                    {
                        if (i == 0)
                        {
                            writer.WriteLine("\tREGION " + iteam.ToList()[i].Group);
                        }

                        if (iteam.ToList()[i].Group2 == @"/")
                        {
                            writer.WriteLine("\t\t\"" + iteam.ToList()[i].Adress + "\"" + " := " + "\"数据块_2\".Output." + iteam.ToList()[i].Group + "." + iteam.ToList()[i].Name + ";" + "  //  " + iteam.ToList()[i].Notes);
                        }
                        else
                        {
                            writer.WriteLine("\t\t\"" + iteam.ToList()[i].Adress + "\"" + " := " + "\"数据块_2\".Output." + iteam.ToList()[i].Group + "." + iteam.ToList()[i].Group2 + "." + iteam.ToList()[i].Name + ";" + "  //  " + iteam.ToList()[i].Notes);
                        }

                        if (i == iteam.Count() - 1)
                        {
                            writer.WriteLine("\tEND_REGION");
                        }
                    }
                }

                writer.WriteLine("END_FUNCTION");
                writer.Close();
            } ;

            
        }
    }
}
